<template>
	<view>
		<view>
			<uni-section title="协会列表" type="line">
				<uni-group mode="card" v-for="(item,index) in dataList" :key="index">
					<view style="display: flex; justify-content: space-between;margin:10rpx 30rpx;align-items: center;">
						<view style="font-size: 28rpx;" @click="goAssociation(item.id)">
							{{item.name}}
						</view>
						<view v-if="associationId == -1">
							<view v-if="applyId == item.id">
								<u-button type="error" text="审核中" :plain="true" size="mini" @click="getToast">
								</u-button>
							</view>
							<view v-else>
								<u-button v-if="associationId != item.id" type="error" text="申请入会" :plain="true"
									size="mini" @click="goToApply(item.id,item.name)">
								</u-button>
							</view>
						</view>
						<view v-else>
							<view v-if="delId == item.id">
								<u-button type="error" text="审核中" :plain="true" size="mini" @click="getToast">
								</u-button>
							</view>
							<view v-else>
								<u-button v-if="associationId == item.id" type="error" text="解除" :plain="true"
									size="mini" @click="goToDel(item.id)">
								</u-button>
								<u-button v-if="associationId != item.id" type="error" text="申请入会" :plain="true"
									size="mini" @click="goToApply(item.id,item.name)">
								</u-button>
							</view>
						</view>
					</view>
				</uni-group>
			</uni-section>
		</view>
		<view style="margin-top: 45rpx;">
			<uni-pagination show-icon :page-size="limit" :current="page" :total="total" @change="change" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mode: 'single',
				dataList: [],
				unitType: 1,
				associationId: 0,
				page: 1,
				limit: 3,
				total: 0,
				applyId: 0,
				delId: 0,
				enterpriseId:''
			}
		},
		onShow() {
			this.applyId = uni.getStorageSync('applyId')
			if (this.associationId == this.applyId) {
				uni.removeStorageSync('applyId')
			}
			this.delId = uni.getStorageSync('delId')
			if (this.associationId == -1) {
				uni.removeStorageSync('delId')
				uni.removeStorageSync('applyId')
			}else if(this.associationId == this.delId){
				uni.removeStorageSync('delId')
			}
		},
		onLoad(options) {
			
			this.associationId = options.id
			this.selectedIndexs = []
			this.getTabList(1)
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				const that = this
				that.$api.request('user', 'info').then(res => {
					this.enterpriseId = res.data.enterpriseId
				})
			},
			// 分页触发
			change(e) {
				this.selectedIndexs.length = 0
				this.getTabList(e.current)
			},
			//协会跳转
			goAssociation(id) {
				uni.navigateTo({
					url: '/pages/index/association?id=' + id
				})
			},
			//渲染协会的数据
			getTabList(pageSize) {
				uni.showLoading({
					title: "正在加载",
				});
				const that = this
				that.page = pageSize
				const introductionsList = []
				that.$api.request('advert', 'company', {
					unitType: that.unitType,
					page: pageSize,
					limit: that.limit
				}).then(res => {
					uni.hideLoading();
					that.dataList = res.data.items
					that.total = res.data.total
				})
			},
			// 申请入会
			goToApply(id, name) {
				if (this.applyId || this.delId) {
					console.log(this.applyId,this.delId);
					uni.showToast({
						title: '正在审核中，无法申请入会',
						icon: 'none',
						duration: 1000
					})
				}
				else {
					uni.navigateTo({
						url: `/pages/address/list?id=` + id + `&name=` + name
					})
				}

			},
			// 解除
			goToDel(id) {
				this.$api.request('examineAndApprove', 'create', {
					associationId: id,
					enterpriseId:this.enterpriseId,
					type: 2
				}).then(res => {
					uni.setStorageSync('delId', id)
					this.delId = uni.getStorageSync('delId')
				})
			},
			getToast() {
				uni.showToast({
					title: '审核中',
					icon: 'none',
					duration: 1000
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
